/*
 * @Author: luoqi
 * @Date: 2022-04-04 13:46:04
 * @LastEditors: luoqi
 * @LastEditTime: 2022-05-05 11:23:08
 * @Description: 
 */
import axios from 'axios';
import nprogress from 'nprogress';
import 'nprogress/nprogress.css'
import { Message } from 'element-ui';
nprogress.configure({ showSpinner: true }) 
// import $$ from 'cmn-utils';
// 创建axios实例
const service = axios.create({
    baseURL: 'http://118.31.71.7:8091',
    timeout: 10000, // 请求超时时间
})

const err=function (error) {
   
    return Promise.reject(error)
}
// 设置请求拦截
service.interceptors.request.use(config => {

    // console.log('发起请求', config)
    const token=JSON.parse(sessionStorage.getItem('token'))
    config.headers['token'] = token 
    nprogress.start()
    // console.log(config)
    return config
  }, err)
  //设置 响应拦截 
service.interceptors.response.use(
    function (response) {
        // console.log(response)
        nprogress.done()
        if(response.data.code==='5003'){
            // 跳转页面
            window.location.href = '/main/login'
            Message.error('登录状态已过期，请重新登录')
        }
        if(response.data.code==='5012'){
            Message.warning('没有数据')
        }
        //请求正常则返回
        if (response.status === 200) {
            return Promise.resolve(response.data)
        } else if (response.status === 403) {
            // 未登录或者登陆失效
            return Promise.resolve(response.data)
        }

        
        notification.error({
            message: response.status,
            description: response.statusText,
        });
    },err
    
)


export default service